package com.gszkc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gszkc.constant.MessageConstant;
import com.gszkc.constant.StatusConstant;
import com.gszkc.dto.SetmealDTO;
import com.gszkc.dto.SetmealPageQueryDTO;
import com.gszkc.entity.Setmeal;
import com.gszkc.entity.SetmealDish;
import com.gszkc.exception.DeletionNotAllowedException;
import com.gszkc.mapper.CategoryMapper;
import com.gszkc.mapper.SetmealDishMapper;
import com.gszkc.mapper.SetmealMapper;
import com.gszkc.result.PageResult;
import com.gszkc.service.SetMealService;
import com.gszkc.vo.DishItemVO;
import com.gszkc.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据ID查询套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询Setmeal数据
        Setmeal setmeal = setmealMapper.getById(id);
        //查询Category的Name
        String categoryName = categoryMapper.getById(setmeal.getCategoryId()).getName();
        //查询SetmealDish表的数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
        //数据封装到VO返回
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setCategoryName(categoryName);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 新增套餐信息 + 套餐和菜品关联表信息
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增套餐表信息
        setmealMapper.insert(setmeal);

        //提取套餐ID循环放入套餐菜品表 (setmealDish赋值)
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish ->
                setmealDish.setSetmealId(setmealId));
        //新增套餐菜品表信息
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pageVo = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(pageVo.getTotal(),pageVo.getResult());
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //更新套餐信息
        setmealMapper.update(setmeal);
        //提取DTO里面的套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //先删除原来套餐里面的关联的套餐菜品信息再增加
        setmealDishMapper.deleteById(setmealDTO.getId());

        if (setmealDishes != null && setmealDishes.size() > 0 ){
            setmealDishes.forEach(setmealDish ->
                    //setmeal和setmealDTO的ID是一样的(copy)
                    setmealDish.setSetmealId(setmeal.getId()));
            //批量插入套餐菜品表
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断ids列表里面有没有起售中的套餐,有则不能删除
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐
        setmealMapper.deleteBatchByIds(ids);
        //删除套餐菜品表
        setmealDishMapper.deleteBatchByIds(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
